﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DomainInfoLib;

namespace DomainInfoDigger
{
    public partial class FrmQueryBuilder : Form
    {
        public FrmQueryBuilder()
        {
            InitializeComponent();
        }

        QueryGroup _QueryGroup = new QueryGroup();

        FrmBatchSearch frmBatchSearch;


        private void FrmQueryBuilder_Load(object sender, EventArgs e)
        {
            
        }




        private void AddToUI(UCLib.UC_ListBox ucList, IList<string> paramsList)
        {
            if (paramsList==null)
            {
                return;
            }
            ucList.SetList(paramsList);
        }



       

        private void button_BuildQueryItems_Click(object sender, EventArgs e)
        {
            BuildQueryGroup();

            DisplayQueryGroupInfo();
        }

        private void DisplayQueryGroupInfo()
        {
            textBox_QueryGroupInfo.Text = "";
            textBox_QueryGroupInfo.Text = "查询项总数：" + _QueryGroup.Count.ToString();
            
        }

        private void BuildQueryGroup()
        {
            _QueryGroup = new QueryGroup();

            _QueryGroup.AddKeyWords(GetKeyWords());

            _QueryGroup.AddDomains(GetCheckedDomains());

            _QueryGroup.AddSurFixes(GetSurFixes());

            _QueryGroup.AddPreFixes(GetPreFixes());

            _QueryGroup.DoQueryKeyWord(GetIsQueryKeyWord());

            _QueryGroup.DoQueryPreFixesAndKeyWord(GetIsPreFixesAndKeyWord());

            _QueryGroup.DoQueryKeyWordsAndSurFixes(GetIsKeyWordsAndSurFixes());

            _QueryGroup.DoQueryPreFixesAndKeyWordsAndSurFixes(GetIsPreFixesAndKeyWordsAndSurFixes());

            _QueryGroup.BuildItems();
        }

        private List<string> GetPreFixes()
        {
            List<string> preFixes = uC_ListBox_PreFixes.GetList() as List<string>;

            return preFixes;
        }

        private List<string> GetKeyWords()
        {
            List<string> keyWords = uC_ListBox_Keywords.GetList() as List<string>;

            return keyWords;
        }

        private List<string> GetCheckedDomains()
        {
            List<string> domains = new List<string>();

            foreach (object item in checkedListBox_Domians.CheckedItems)
            {
                domains.Add(item.ToString());
            }
            return domains;
        }

        private List<string> GetSurFixes()
        {
            List<string> surFixes = uC_ListBox_SurFixes.GetList() as List<string>;

            return surFixes;
        }

        private bool GetIsPreFixesAndKeyWordsAndSurFixes()
        {
            return checkBox_PFixAndKeyAndSFix.Checked;
        }

        private bool GetIsKeyWordsAndSurFixes()
        {
            return checkBox_KeyWordAndSurFix.Checked;
        }

        private bool GetIsPreFixesAndKeyWord()
        {
            return checkBox_PreFixAndKeyWord.Checked;
        }

        private bool GetIsQueryKeyWord()
        {
            return checkBox_OnlyKeword.Checked;
        }

        private void button_GoQuery_Click(object sender, EventArgs e)
        {
            this.Hide();

            frmBatchSearch = new FrmBatchSearch();

            frmBatchSearch.SizeChanged += new EventHandler(frmBatchSearch_SizeChanged);
            
            frmBatchSearch.SetQueryGroup(_QueryGroup);

            frmBatchSearch.ShowDialog();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button==MouseButtons.Left)
            {
                Restore();
            }
            
        }

        void frmBatchSearch_SizeChanged(object sender, EventArgs e)
        {
            if (frmBatchSearch.WindowState==FormWindowState.Minimized)
            {
                HideApp();
            }
        }

        private void HideApp()
        {
            if (frmBatchSearch != null) frmBatchSearch.Visible = false;
            this.Hide();
            this.notifyIcon1.Visible = true;
            this.ShowInTaskbar = false;
        }

        private void Restore()
        {
            try
            {
                if (frmBatchSearch != null)
                {
                    frmBatchSearch.WindowState = FormWindowState.Normal;
                    frmBatchSearch.Visible = true;
                    frmBatchSearch.Activate();
                }
                this.Visible = true;
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
                this.notifyIcon1.Visible = false;
                this.ShowInTaskbar = true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                Application.Exit();
            }

        }

        private void FrmQueryBuilder_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                HideApp();
            }
        }



        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                notifyIcon1.ShowBalloonTip(1000, "搜索状态",GetTipTxt(), ToolTipIcon.Info);
            }
        }

        private string GetTipTxt()
        {
            if (frmBatchSearch==null)
            {
                return "待命中";
            }

            return frmBatchSearch.SearchState();


        }
    }
}
